import { _decorator, Color, Component, Label, Node, UITransform, v3 } from 'cc';
const { ccclass, property } = _decorator;

import ChengYu_GameConfig from "../Data/ChengYu_GameConfig";
import { ChengYu_GameData } from "../Data/ChengYu_GameData";


@ccclass('XXLGrid')
export default class XXL_Grid extends Component {
    @property(Label)
    Label_word: Label | null = null;
    @property(Node)
    select: Node | null = null;
    @property(Label)
    Label_index: Label | null = null;
    @property(Node)
    normal: Node | null = null;
    @property(Node)
    empty: Node | null = null;
    @property(Node)
    right: Node | null = null;
    @property(Node)
    wrong: Node | null = null;
    id: number = 0;
    x: number = 0;
    y: number = 0;
    index: number = -1;
    state = "empty";
    word_right: string;
    word_current: string;
    isActive: boolean = false;
    isFill: boolean = true;
    isRight: boolean = true;
    isComplete: boolean = false;
    //    // 初始化
    init(_id: number, _x: number, _y: number) {
        this.id = _id;
        this.x = _x;
        this.y = _y;

        this.initWidthAndHeight(ChengYu_GameConfig.grid_size, ChengYu_GameConfig.grid_size);
        this.initPosition();

        this.Refresh();
        this.AddListener();
        this.Select(false);

        this.Label_index.node.active = false;
    }
    AddListener() {
        this.node.on(Node.EventType.TOUCH_START, this.On_TOUCH_START, this);
    }
    On_TOUCH_START() {
        // xwLog(this.x, this.y, this.id);
    }
    Refresh() {
        // this.node.active = this.id >= 0;

        this.normal.active = false;
        this.empty.active = false;
        this.right.active = false;
        this.wrong.active = false;

        if (this.isActive === true) {
            if (this.isFill === true) {
                // this.wrong.active = true;
                if (this.word_right === this.word_current) {
                    this.right.active = true;

                    this.Label_word.color = new Color().fromHEX("#FFFFFF");
                } else {
                    this.wrong.active = true;
                }
            } else {
                this.empty.active = true;
            }
        } else {
            if (this.isComplete === true) {
                this.right.active = true;

                this.Label_word.color = new Color().fromHEX("#FFFFFF");
            } else {
                this.normal.active = true;
            }
        }
    }
    Select(select: boolean): void {
        this.select.active = select;
    }
    //    // 初始化单元宽高
    initWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    initPosition() {
        const w = ChengYu_GameConfig.size;
        const h = ChengYu_GameConfig.size;
        let x = -ChengYu_GameConfig.W / 2 + w / 2 + w * this.x;
        let y = -ChengYu_GameConfig.H / 2 + h / 2 + h * this.y;

        let getX = this.GetX();
        let getY = this.GetY() - 0.15;
        x += (getX - 0.5) * ChengYu_GameConfig.size;
        y += (getY - 0.5) * ChengYu_GameConfig.size;

        this.node.position = v3(x, y, 0);
    }
    GetX() {
        let posx = ChengYu_GameData.GameData.LevelData.posx;
        const arr = posx.concat();

        let newArr = arr.sort(function (a, b) {
            return a - b;
        });

        let x_min = arr[0];
        let x_max = arr[arr.length - 1];
        let x_middle = (x_max - x_min) / 2;
        let x_main = ChengYu_GameConfig.col;
        let x_main_middle = x_main / 2;
        let x_diff = x_main_middle - x_middle;
        let x_new = x_min + x_diff;

        return x_new;
    }
    GetY() {
        let posx = ChengYu_GameData.GameData.LevelData.posy;
        const arr = posx.concat();

        let newArr = arr.sort(function (a, b) {
            return a - b;
        });

        let x_min = arr[0];
        let x_max = arr[arr.length - 1];
        let x_middle = (x_max - x_min) / 2;
        let x_main = ChengYu_GameConfig.row;
        let x_main_middle = x_main / 2;
        let x_diff = x_main_middle - x_middle;
        let x_new = x_min + x_diff;

        return x_new;
    }
}

// /** 调试 */
window["XXL_Grid"] = XXL_Grid;